package com.hangzhou.welbeing.welbeinginstrument.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.EveryDayArticlesListAdapter;
import com.hangzhou.welbeing.welbeinginstrument.bean.ReadingEveryDayDateBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemClickListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 时间 2017 年 10 月 03.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.welbeinginstrument.activity
 * 工程名 welbeing_pregnant
 */
public class EveryDayArticlesListActivity extends Activity {
    @Bind(R.id.activity_every_day_articles_list_iv_return)
    ImageView activityEveryDayArticlesListIvReturn;
    @Bind(R.id.activity_every_day_articles_list_iv_shared)
    ImageView activityEveryDayArticlesListIvShared;
    @Bind(R.id.activity_every_day_articles_list_recylerview)
    RecyclerView activityEveryDayArticlesListRecylerview;
    private List<ReadingEveryDayDateBean.DataBean> mlist1;
    private EveryDayArticlesListAdapter everyDayArticlesListAdapter;

    private int p = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_every_day_articles_list);
        ButterKnife.bind(this);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        activityEveryDayArticlesListRecylerview.setLayoutManager(layoutManager);


        loadEveryDayArticlesListDate(p);
    }

    @OnClick({R.id.activity_every_day_articles_list_iv_return, R.id.activity_every_day_articles_list_iv_shared})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_every_day_articles_list_iv_return:
                finish();
                break;
            case R.id.activity_every_day_articles_list_iv_shared:
                break;
        }
    }

    /**
     * @desc 加载每日一读全部数据
     * */
    private void loadEveryDayArticlesListDate(int p) {
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL + RBConstants.READING_EVERY_DAY)
                .addParams("p", p + "")
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String parseNetworkResponse = response.body().string();
                        // Log.e(TAG,"parseNetworkResponse========"+parseNetworkResponse);
                        Gson gson = new Gson();
                        final ReadingEveryDayDateBean readingEveryDayDateBean = gson.fromJson(parseNetworkResponse, ReadingEveryDayDateBean.class);
                        if (readingEveryDayDateBean.errorCode == 200) {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    mlist1 = readingEveryDayDateBean.data;
                                    if (mlist1 != null) {
                                        /**-------每日一读--------*/
                                        everyDayArticlesListAdapter = new EveryDayArticlesListAdapter(mlist1, EveryDayArticlesListActivity.this);
                                        activityEveryDayArticlesListRecylerview.setAdapter(everyDayArticlesListAdapter);
                                        everyDayArticlesListAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
                                            @Override
                                            public void onItemClick(View view) {
                                                int position = activityEveryDayArticlesListRecylerview.getChildAdapterPosition(view);
                                                Intent intent = new Intent(EveryDayArticlesListActivity.this, ArticlesActivity.class);
                                                intent.putExtra("arit_id", mlist1.get(position).id);
                                                startActivity(intent);

                                            }

                                            @Override
                                            public void onItemFirstButtonClick(View v, int position) {

                                            }
                                        });
                                    }
                                }
                            });
                        } else {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EveryDayArticlesListActivity.this, readingEveryDayDateBean.errorMessage, Toast.LENGTH_LONG).show();
                                }
                            });
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

}
